package com.sakiko._02_springai_chatclient.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * projectName: com.sakiko._02_springai_chatclient.controller
 *
 * @author: Yang
 * description: 角色预设
 * time: 2025/8/7 22:58
 */
@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private ChatClient client;

    // 非流式输出 call: 等待大模型把回答结果全部生成后输出给用户
    @GetMapping(value = "/call", produces = "text/html;charset=UTF-8")
    public String chat(@RequestParam(name = "msg", defaultValue = "who are you") String message) {
        return client.prompt()
                .user(message)
                .call()
                .content();
    }

    // 流式输出 stream: 逐个字符输出，推理效果不高时，流式输出可以提高用户体验
    // 设置编码格式，避免 GBK字符集，中文出现乱码问题
    @GetMapping(value = "/stream", produces = "text/html;charset=UTF-8")
    public Flux<String> chat2(@RequestParam(name = "msg", defaultValue = "who are you") String message) {
        return client.prompt()
                .user(message)
                .stream()
                .content();
    }
}
